package com.madou.dbtool.redis.panel.dataloader;

import com.madou.common.annotation.HideClass;
import com.madou.dbtool.common.dto.Complex;
import com.madou.dbtool.redis.manager.RedisSourceManager;
import com.madou.dbtool.redis.utils.BytesToStringUtil;
import com.madou.dbtool.ui.table.MColumn;
import com.madou.dbtool.ui.table.MRow;
import com.madou.dbtool.ui.table.enums.RowType;
import com.madou.dbtool.ui.table.loader.TableDataLoader;
import com.madou.dbtool.ui.table.row.MObjectRow;
import redis.clients.jedis.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * redis扫描keys
 * @author miukoo
 */
@HideClass
public class RedisListTableDataLoader implements TableDataLoader {

    private final static String HGETALL = "LRANGE %s 0 2000";
    RedisSourceManager redisSourceManager;
    String selectKey;
    int dbIndex;
    public RedisListTableDataLoader(RedisSourceManager redisSourceManager, String selectKey,int dbIndex){
        this.redisSourceManager = redisSourceManager;
        this.selectKey = selectKey;
        this.dbIndex = dbIndex;
    }

    @Override
    public Complex<Integer, List<MRow>> load(Map<String, Object> searchKeyword, int page, int size) {
        List<byte[]> execute = (List<byte[]>) redisSourceManager.execute(dbIndex,String.format(HGETALL,selectKey));
        List<MRow> collect = new ArrayList<>();
        if(execute!=null){
            for (int i = 0; i < execute.size(); i++) {
                collect.add(new MObjectRow(new Complex<Integer,String>(i+1,new String(execute.get(i)))));
            }
        }
        return new Complex<Integer, List<MRow>>(execute.size(),collect);
    }

    /**
     * 表格数据进行持久化保存
     * @param columns
     * @param updatedOrInsertRow
     * @param deletedRow
     */
    @Override
    public void applyChanged(List<MColumn> columns, List<MRow> updatedOrInsertRow, List<MRow> deletedRow) throws Exception {
        if(updatedOrInsertRow.size()>0){
            for (MRow mRow : updatedOrInsertRow) {
                if(mRow.getRowType()== RowType.NEWLINE){
                    byte[][] parm = new byte[2][1];
                    parm[0]= BytesToStringUtil.string2Byte(selectKey);
                    parm[1]= mRow.getColumn("second").toString().getBytes();
                    redisSourceManager.execute(dbIndex, Protocol.Command.LPUSH,parm);
                }else if(mRow.isUpdated()){
                    int index  = Integer.valueOf(mRow.getColumn("first").toString())-1;
                    byte[][] parm = new byte[2][1];
                    parm[0]= BytesToStringUtil.string2Byte(selectKey);
                    parm[1]= (""+index).getBytes();
                    parm[2]= mRow.getColumn("second").toString().getBytes();
                    redisSourceManager.execute(dbIndex, Protocol.Command.LSET,parm);
                }
            }
        }
        if(deletedRow.size()>0){
            for (MRow mRow : deletedRow) {
                int index  = Integer.valueOf(mRow.getColumn("first").toString())-1;
                byte[][] parm = new byte[2][1];
                parm[0]= BytesToStringUtil.string2Byte(selectKey);
                parm[1]= (""+index).getBytes();
                parm[2]= mRow.getColumn("second").toString().getBytes();
                redisSourceManager.execute(dbIndex, Protocol.Command.LREM,parm);
            }
        }
    }

}
